package jgl.vajra.management.part.mine.fragment;

import android.text.TextUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.management.R;
import jgl.vajra.management.bean.MyCarListBean;
import jgl.vajra.management.dialogFragment.AuditDetailsDialogFragment;
import jgl.vajra.management.dialogFragment.AuthorizationPromptDialogFragment;
import jgl.vajra.management.dialogFragment.CertificationAuditDialogFragment;
import jgl.vajra.management.event.MyCarListEvent;
import jgl.vajra.management.event.OpenCityEvent;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.mine.contract.MyCarListContract;
import jgl.vajra.management.part.mine.viewModel.MyCarListViewModel;
import jgl.vajra.management.widget.base.BaseListFragment;
import jgl.vajra.management.widget.bean.BaseListData;
import jgl.vajra.management.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(MyCarListViewModel.class)
public class MyCarListFragment extends BaseListFragment<MyCarListViewModel, String> implements MyCarListContract.View {

    private String authorization;
    private int type;
    private int tag;
    private String whereEnd;
    private CertificationAuditDialogFragment certificationAuditDialogFragment;
    private int internal;
    private AuditDetailsDialogFragment auditDetailsDialogFragment;

    @Override
    protected RefreshRecyclerNetConfig getRefreshRecyclerNetConfig() {
        return new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", authorization);
                map.put("type", type);
                map.put("tag", tag);
                map.put("internal", internal);
                if (!TextUtils.isEmpty(whereEnd))
                    map.put("whereEnd", whereEnd);
                return mViewModel.getMyCarList(map);
            }
        };
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_my_car;
    }

    @Override
    public void onItemClick(int position, String itemData) {

    }

    @Override
    public void initView() {
        super.initView();
        type = getArguments().getInt("type");
        tag = getArguments().getInt("tag");
        internal = getArguments().getInt("internal");
        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<MyCarListBean>>() {
            @Override
            public void onSucceed(BaseListData<MyCarListBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);
                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();

        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
        mBinding.recyclerView.setRootViewBackgroundRes(R.color.gray_f7);
    }


    //审核详情
    @Override
    public void onAuditDetails(MyCarListBean itemData) {
        if (itemData.payState == 2) {
            if (auditDetailsDialogFragment == null)
                auditDetailsDialogFragment = new AuditDetailsDialogFragment();
            auditDetailsDialogFragment.setData(itemData);
            auditDetailsDialogFragment.show(mActivity.getSupportFragmentManager());
        }
    }

    //详情
    @Override
    public void onDetails(MyCarListBean itemData) {
        IntentManager.toCarDetailsActivity(mActivity, itemData.logo, itemData.model, tag);
    }

    //审核
    @Override
    public void onAudit(MyCarListBean itemData) {
        certificationAuditDialogFragment = new CertificationAuditDialogFragment();
        certificationAuditDialogFragment.setData(tag, itemData.logo);
        certificationAuditDialogFragment.setCertificationAuditListener(new CertificationAuditDialogFragment.CertificationAuditListener() {
            @Override
            public void onDetermine() {
                mBinding.recyclerView.firstLoad();
            }
        });
        certificationAuditDialogFragment.show(mActivity.getSupportFragmentManager());
    }

    @Override
    public void onResume() {
        super.onResume();
        mBinding.recyclerView.firstLoad();
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MyCarListEvent(MyCarListEvent event) {
        whereEnd = event.whereEnd;
        mBinding.recyclerView.firstLoad();
    }
}
